package org.example.redpacket.service.impl;

import org.example.redpacket.common.exception.AlreadyInsertException;
import org.example.redpacket.dao.UserRedPacketMapper;
import org.example.redpacket.model.entity.UserRedPacketEntity;
import org.example.redpacket.model.po.UserRedPacketPO;
import org.example.redpacket.service.UserRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: hero生仔
 */
@Service
public class UserRedPacketServiceImpl implements UserRedPacketService {

    @Autowired
    private UserRedPacketMapper userRedPacketMapper;

    @Override
    public Integer insertUserRedPacket(UserRedPacketEntity userRedPacketEntity) {
        UserRedPacketPO userRedPacketPO = UserRedPacketPO.builder()
                .activityId(userRedPacketEntity.getActivityId())
                .userId(userRedPacketEntity.getUserId())
                .number(userRedPacketEntity.getNumber())
                .build();
        Integer insertNum;
        try {
            if (userRedPacketEntity.getNumber() == 1) {
                insertNum = userRedPacketMapper.insertUserRedPacket(userRedPacketPO);
            } else {
                insertNum = userRedPacketMapper.updateUserRedPacket(userRedPacketPO);
            }
        } catch (Exception e) {
            throw new AlreadyInsertException(e.getLocalizedMessage(), e);
        }
        return insertNum;
    }


    @Override
    public Integer queryReceiveRedPacketCount(Map<String, Object> map) {
        return userRedPacketMapper.queryReceiveRedPacketCount(map);
    }
}
